package com.vim.modules.mall.controller;

import com.vim.common.base.BaseController;
import com.vim.common.utils.POJOConverter;
import com.vim.modules.mall.model.mallGoods.MallGoodsType;
import com.vim.modules.mall.model.mallGoods.MallGoodsTypeTree;
import com.vim.modules.mall.service.MallGoodsService;
import com.vim.modules.sys.model.ZTreeNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.vim.common.constants.Global.LONG_TREE_ROOT_ID;
import static com.vim.common.constants.Global.TREE_ROOT_ID;

/**
 * @作者 Administrator
 * @时间 2020-09-06 13:36
 * @版本 1.0
 */
@Controller
@RequestMapping(value = "/mall/goodsType")
public class MallGoodsTypeController extends BaseController{

    @Autowired
    private MallGoodsService mallGoodsService;

    /**
     * 商品分类列表页面
     */
    @RequiresPermissions(value = "mall:goodsType:list")
    @RequestMapping(value = "/list")
    public String list(){
        return "mall/goods/goodsTypeList";
    }

    /**
     * 商品分类列表数据
     */
    @RequiresPermissions(value = "mall:goodsType:list")
    @RequestMapping(value = "/dataList")
    @ResponseBody
    public ResponseEntity dataList(@RequestParam(value = "nodeid", required = false) String parentId,
                                                        @RequestParam(value = "goodsTypeName", defaultValue = "") String goodsTypeName){
        List<MallGoodsTypeTree> result = new ArrayList<>();
        Long pid = StringUtils.isBlank(parentId) ? Long.valueOf(TREE_ROOT_ID) : Long.valueOf(parentId);
        mallGoodsService.goodsTypeListWithParent(pid).forEach(type->{
            MallGoodsTypeTree node = POJOConverter.copy(type, MallGoodsTypeTree.class);
            node.setParentId(type.getPid());
            node.setLevel(Long.valueOf(type.getPath().split("\\.").length-2));
            node.setLeaf(type.getPath().split("\\.").length == 4 ? true : false);
            result.add(node);
        });
        return new ResponseEntity<>(success(result), HttpStatus.OK);
    }

    /**
     * 树列表数据
     */
    @RequestMapping(value = "/ztreeSelectData")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> ztreeSelectData(){
        List<ZTreeNode> result = new ArrayList<>();
        mallGoodsService.goodsTypeList().forEach(type->{
            result.add(new ZTreeNode(type.getId(), type.getPid(), type.getName()));
        });
        return new ResponseEntity<>(success(result), HttpStatus.OK);
    }

    /**
     * 编辑页面
     */
    @RequiresPermissions(value = "mall:goodsType:form")
    @RequestMapping(value = "/form")
    public String form(MallGoodsType mallGoodsType, Model model){
        MallGoodsType goodsType = null;
        MallGoodsType parentGoodsType = null;
        if(null != mallGoodsType.getId()){
            goodsType = mallGoodsService.goodsTypeInfo(mallGoodsType.getId());
            parentGoodsType = LONG_TREE_ROOT_ID == mallGoodsType.getPid() ? null : mallGoodsService.goodsTypeInfo(goodsType.getPid());
        }
        model.addAttribute("goodsType", null == goodsType ? new MallGoodsType(LONG_TREE_ROOT_ID, TREE_ROOT_ID) : goodsType);
        model.addAttribute("parentGoodsTypeName", null == parentGoodsType ? "无上级" : parentGoodsType.getName());
        return "mall/goods/goodsTypeForm";
    }
}
